package com.sgb.member.view_model;

import android.text.TextUtils;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.sgb.member.MemberConfig;
import com.sgb.member.di.repository.MemberRepertory;
import com.sgb.member.entity.ListMResponse;
import com.sgb.member.entity.QualityMemberBean;
import com.sgb.member.ui.fragment.MemberInviteOrApplyFragment;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;
import com.swgk.core.util.StringUtil;
import com.swgk.core.view.areapopup.AddressEntity;
import com.swgk.core.view.selection.CommonPopEntity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;

public class MemberInviteOrApplyModel2 extends ViewModel {

    /**
     * 通知主页面数据刷新
     */
    public MutableLiveData<String> message = new MutableLiveData<>("");
    public MutableLiveData<String> getMessage() {
        return message;
    }
    public MutableLiveData<Boolean> isLoading = new MutableLiveData<>();
    public MutableLiveData<List<QualityMemberBean>> listLiveData = new MutableLiveData<>();
    public MutableLiveData<List<QualityMemberBean>> listLiveData2 = new MutableLiveData<>();
    public MutableLiveData<Boolean> isEmptyLiveData = new MutableLiveData<>();
    public MutableLiveData<String> statusLiveData = new MutableLiveData<>();
    public MutableLiveData<List<CommonPopEntity>> dataLiveData1 = new MutableLiveData<>();
    public MutableLiveData<List<CommonPopEntity>> dataLiveData2 = new MutableLiveData<>();

    int current;

    /**
     * 获取企业列表
     *
     * @param curPageType 0 邀请会员；1申请会员
     * @param isLoadMore  是否加载更多
     */
    public void getInviteOrApplyCompanyList(int curPageType, boolean isLoadMore, String text, String businessNo, String areaNo) {
        Call<BaseEntity<ListMResponse<List<QualityMemberBean>>>> call;
        isLoading.postValue(true);
        int currentPage = isLoadMore ? current + 1 : 1;
        Map<String, Object> map = new HashMap<>();
        map.put("name", StringUtil.isEmpty(text) ? "" : text); // 企业名称
        map.put("type", businessNo);
        map.put("area", areaNo);
        map.put("limit", 20);
        map.put("page", currentPage);
        if (curPageType == 0) {
            map.put("joinWay", "2"); //加盟方式  1申请 2邀请
            if (MemberConfig.getInstance().getComType() == MemberConfig.WEBSITE_FOR_PERSONAL) {
                call = MemberConfig.getInstance().getApi().getPersonApplyCompanyList(map);
            } else {
                call = MemberConfig.getInstance().getApi().getInviteCompanyList(map);
            }
        } else {
            map.put("joinWay", "1"); //加盟方式  1申请 2邀请
            if (MemberConfig.getInstance().getComType() == MemberConfig.WEBSITE_FOR_PERSONAL) {
                call = MemberConfig.getInstance().getApi().getPersonApplyCompanyList(map);
            } else {
                call = MemberConfig.getInstance().getApi().getApplyCompanyList(map);
            }
        }
        call.enqueue(new ICallBack<BaseEntity<ListMResponse<List<QualityMemberBean>>>>() {
            @Override
            public void onSuccess(BaseEntity<ListMResponse<List<QualityMemberBean>>> data) {
                current = data.getData().getCurrent();
                int total = data.getData().getPages();
                if (current >= total){
                    listLiveData.postValue(data.getData().getList());
                }else {
                    listLiveData2.postValue(data.getData().getList());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListMResponse<List<QualityMemberBean>>>> call, Throwable t) {
                super.onFailure(call, t);
                isEmptyLiveData.postValue(true);
            }
        });
    }

    /**
     * 获取审批流程状态
     * <p>
     * 邀请加盟审批类型 inviteJoinCompany
     * 申請加盟审批类型 applyJoinCompany
     * 会员企业调级 memberEvaluate
     * 会员解除申请 memberRelieve
     */
    public void getProcessStatus(String companyNo, String approveType) {
        isLoading.postValue(true);
        MemberConfig.getInstance().getApi().getProcessStatus(companyNo, approveType).enqueue(new ICallBack<BaseEntity<Object>>() {
            @Override
            public void onSuccess(BaseEntity<Object> data) {
                // "status": "1" //1 表示有审批 0 代表无审批
                String status = ((Map<String, String>) data.getData()).get("status");
                if (!TextUtils.isEmpty(status)) {
                    statusLiveData.postValue(status);
                } else {
                    message.postValue("网络异常，请稍后重试");
                }
            }
        });
    }

    public void getBusinessTypes(int type) {
        isLoading.postValue(true);
        MemberConfig.getInstance().getApi().getServiceTypeList().enqueue(new ICallBack<BaseEntity<List<CommonPopEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<CommonPopEntity>> data) {
                if (data != null && data.getData() != null && data.getData().size() > 0) {
                    dataLiveData1.postValue(data.getData());
                }
            }
        });
    }

    /**
     * 获取省市区地址
     */
    public void getAddressModel(int type) {
        isLoading.postValue(true);
        MemberConfig.getInstance().getApi().getAddressEntityCityList().enqueue(new ICallBack<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>>() {
            @Override
            public void onSuccess(BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>> data) {
                isLoading.postValue(false);
                if (data != null && data.getData() != null && data.getData().size() > 0) {
                    List<CommonPopEntity> entities = new ArrayList<>();
                    for (AddressEntity<List<AddressEntity<List<AddressEntity>>>> datum : data.getData()) {
                        entities.add(new CommonPopEntity(datum.getNo(), datum.getSimpleName()));
                    }
                    entities.add(0, new CommonPopEntity("", "全国"));
                    dataLiveData2.postValue(entities);
                }
            }
        });
    }
}
